package com.foodchain.service;

import com.foodchain.consts.Code;
import com.foodchain.entity.ActivitySchedule;
import com.foodchain.entity.User;
import com.foodchain.repository.BaseUserRepository;
import com.foodchain.util.Md5SaltTool;
import com.foodchain.util.Misc;
import com.foodchain.vo.Register;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.Transient;
import javax.persistence.criteria.*;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

@Service
public class UserService {

    @Resource
    private BaseUserRepository repository;

    public void save(User user) {
        repository.save(user);
    }

    public User findByUserName(String userName) {
        return repository.findByUserName(userName);
    }

    public User getById(String id) { return repository.getOne(id); }

    @Transactional
    public User saveRegisterUser(Register register) {

        User user = new User();
        user.setUserName(register.getUserName());
        user.setSalt(null);
        user.setErrorTimes(0);
        user.setHeadImg("");
        user.setNickName("");
        user.setLastLoginTime(null);
        user.setCreateTime(new Date());
        user.setUserState(Code.UserState.NORMAL);
        user.setVerifyState(Code.VerifyState.UN_VERIFY);
        user.setPassWord(null);
        user.setIntegral(0);
        User saveUser = repository.save(user);

        return saveUser;
    }
    @Transactional
    public User saveRegister(Register register) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        User user = new User();
        user.setUserName(register.getUserName());
        user.setErrorTimes(0);
        user.setHeadImg("");
        user.setNickName("");
        user.setLastLoginTime(null);
        user.setCreateTime(new Date());
        user.setUserState(Code.UserState.NORMAL);
        user.setVerifyState(Code.VerifyState.UN_VERIFY);
        user.setPassWord(Md5SaltTool.getEncryptedPwd(register.getPassWord()));
        user.setIntegral(0);
        User saveUser = repository.save(user);
        return saveUser;

    }

    public Page<User> list(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "integral");
        Page<User> all = repository.findAll(pageable);
        if(all.getContent().size()>0){
            for(User user : all){
                user.setPassWord(null);
                user.setSalt(null);
                user.setCreateTime(null);
            }
        }
        return all;
    }


    public User getByUserId(String userId) {
        Integer integral = repository.getByUserId(userId);
        User user=new User();
        user.setIntegral(integral);
        return user;
    }
}
